package com.letou.ls.view;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.table.Col;
import org.apache.table.TableTitle;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableBottomTileHappyTen extends TableLayout {
    private static final String TAG = "TableView";
    private TableTitle tableTitle;
    
    private int cellBorder = 1;
    private int tableHeadBackgroundColor = Color.parseColor("#3962b4");
    private int tableHeadTextColor = Color.WHITE;
    private Typeface tableHeadTypeface = Typeface.create(Typeface.SERIF, Typeface.BOLD);
    private int tableHeadGravity = Gravity.CENTER;
    // private int baseH = 40;
    private float mLeafTextSize;
    private TableRow titleRow;
    private ArrayList<Float> mWidthList;
    private float density = 3f;
    
    public ArrayList<Float> getmWidthList() {
        return mWidthList;
    }
    
    public void setmWidthList(ArrayList<Float> mWidthList) {
        this.mWidthList = mWidthList;
    }
    
    public float getmLeafTextSize() {
        return mLeafTextSize;
    }
    
    public void setmLeafTextSize(float mLeafTextSize) {
        this.mLeafTextSize = mLeafTextSize;
    }
    
    public TableBottomTileHappyTen(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    public void addData(TableTitle tableTitle, float density) {
        this.setBackgroundColor(Color.RED);
        this.setBaselineAligned(true);
        
        this.density = density;
        this.tableTitle = tableTitle;
        drawTableTitle();
        invalidate();
    }
    
    public void drawTableTitle() {
        /**
         * 画表头
         */
        
        if (tableTitle != null) {
            Col c1 = tableTitle.getDataCol();
            
            if (c1.getmChildList() == null || c1.getmChildList().size() == 0
                    || c1.getmChildList().get(0).getmTextSize() <= 1) {
                TextView text = new TextView(this.getContext());
                text.setText("没有数据");
                text.setTextColor(Color.BLACK);
                text.setGravity(Gravity.CENTER);
                text.setTextSize(20);
                this.setBackgroundColor(Color.TRANSPARENT);
                this.addView(text);
                return;
            }
            TableLayout tableTlayout = new TableLayout(this.getContext());
            tableTlayout.setBaselineAligned(true);
            titleRow = new TableRow(this.getContext());
            int size = c1.getmChildList().size();
            int j = 0;
            for (int i = 0; i < size; i++) {
                j++;
                // c1.getmChildList().get(i).setmDataWidth(mWidthList.get(i));
                Col col = c1.getmChildList().get(i);
                // Logger.v(TAG, " " + col.getmColName() + " " +
                // col.getmNumber());
                TableLayout tl;
                
                if (j == 5) {
                    tl = drawLayers(col, 24);
                } else if (j == 6) {
                    tl = drawLayers(col, 25);
                } else if (j == 7) {
                    tl = drawLayers(col, 26);
                } else if (j == 8) {
                    tl = drawLayers(col, 27);
                }  else if (j == 1) {
                    tl = drawLayers(col, 0);
                } else {
                    tl = drawLayers(col, j);
                }
                if (tl != null) {
                    titleRow.addView(tl);
                }

            }
            tableTlayout.addView(titleRow);
            this.addView(tableTlayout);
        }
    }
    
    /**
     * 画表头方法
     *
     * @param col
     * @return
     */
    private TableLayout drawLayers(Col col, int i) {
        int colChildNum = col.getmChildrenSize();
        int height;
        //Log.e("lb", "drawLayers没孩子节点type:" + mWidthList.get(i) + ";" + i+";"+col.getmColName());
        String type = col.getStye();
        if (i<mWidthList.size()&&mWidthList.get(i) == 0) {
            return null;
        }

        // 没有孩子节点
        if (colChildNum == 0) {
        	 col.setmDataWidth(mWidthList.get(i));
            TableLayout tableLayout = new TableLayout(titleRow.getContext());
            TableRow tableRow = new TableRow(this.getContext());
            
            // tableRow.setPadding(0, 0, cellBorder, cellBorder);
            tableRow.setPadding(0, 0, cellBorder, 0);
            if (mLeafTextSize <= 0) {
                mLeafTextSize = col.getmTextSize();
            }
            height = (int) (col.getmRowspan() * (mLeafTextSize + 1.5) * 1.5 * density);
            
            // Logger.v(TAG, "1 " + height);
            TextView textView = new TextView(this.getContext());
            textView.setWidth((int) (col.getmDataWidth()));
            textView.setHeight(height);
            textView.setGravity(tableHeadGravity);
            if (TextUtils.isEmpty(col.getmBgColor())) {
                textView.setBackgroundColor(tableHeadBackgroundColor);
            } else {
                textView.setBackgroundColor(Color.parseColor(col.getmBgColor()));
            }
            // textView.setTypeface(tableHeadTypeface);
            textView.setText(col.getmColName());
            if (TextUtils.isEmpty(col.getmTextColor())) {
                textView.setTextColor(tableHeadTextColor);
            } else {
                textView.setTextColor(Color.parseColor(col.getmTextColor()));
            }
            if (!isNumeric(col.getmColName())) {
                textView.setTypeface(tableHeadTypeface);
            }
            
            tableLayout.setBackgroundColor(Color.WHITE);
            if ("1".equals(type)) {
                
                textView.setTextSize(mLeafTextSize * 1.3f);
            } else if ("2".equals(type)) {
                
                textView.setTextSize(mLeafTextSize * 0.8f);
            } else if ("3".equals(type)) {
                
                tableLayout.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize);
            } else if ("4".equals(type)) {
                
                tableLayout.setBackgroundColor(Color.parseColor("#FF7F00"));
                // tableLayout.setBackgroundColor(Color.YELLOW);
                textView.setTextSize(mLeafTextSize);
            } else if ("5".equals(type)) {
                
                tableLayout.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize * 1.3f);
            } else if ("6".equals(type)) {
                
                tableLayout.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize * 0.8f);
            } else {
                
                textView.setTextSize(mLeafTextSize);
            }
            tableRow.addView(textView);
            tableLayout.addView(tableRow);
            // tableLayout.setBackgroundColor(Color.RED);
            return tableLayout;
            
        } else {
            // 有孩子节点
            TableLayout tableLayout = new TableLayout(this.getContext());
            
            TableRow tableRow = new TableRow(this.getContext());
            // tableRow.setPadding(0, 0, 0, cellBorder);
            TableRow.LayoutParams tl = new TableRow.LayoutParams();
            if (mLeafTextSize <= 0) {
                mLeafTextSize = col.getmTextSize();
            }
            height = (int) (col.getmRowspan() * (mLeafTextSize + 1.5) * 1.5 * density);
            // Logger.v(TAG, "2 " + height);
            tl.span = colChildNum;
            tl.setMargins(0, 0, 1, 0);
            TextView textView = new TextView(this.getContext());
            // textView1.setWidth(baseH * col.getmColspan());
            textView.setLayoutParams(tl);
            textView.setHeight(height);
            textView.setText(col.getmColName());
            textView.setWidth((int) (col.getmDataWidth() - 1));
            textView.setGravity(tableHeadGravity);
            textView.setTypeface(tableHeadTypeface);
            if (TextUtils.isEmpty(col.getmBgColor())) {
                textView.setBackgroundColor(tableHeadBackgroundColor);
            } else {
                textView.setBackgroundColor(Color.parseColor(col.getmBgColor()));
            }
            if (TextUtils.isEmpty(col.getmTextColor())) {
                textView.setTextColor(tableHeadTextColor);
            } else {
                textView.setTextColor(Color.parseColor(col.getmTextColor()));
            }
            
            TableRow tableRow2 = new TableRow(this.getContext());
            tableRow2.setPadding(0, 1, 0, cellBorder);
            
            for (int j = 0; j < colChildNum; j++) {
                Col colChild = col.getmChildList().get(j);
                colChild.getmDataWidth();
                TableLayout tll = drawLayers(colChild, i);
                
                tableRow2.addView(tll);
            }
            tableRow.addView(textView);
            tableLayout.addView(tableRow);
            tableLayout.addView(tableRow2);
            // tableLayout.setBackgroundColor(Color.RED);
            // * title 1大字号 , 2 小字号 , 3 红色右侧线 ,4 黄色右侧线 , 5大号 红色线,6 小号红色线
            tableLayout.setBackgroundColor(Color.WHITE);
            if ("1".equals(type)) {
                
                textView.setTextSize(mLeafTextSize * 1.3f);
            } else if ("2".equals(type)) {
                
                textView.setTextSize(mLeafTextSize * 0.8f);
            } else if ("3".equals(type)) {
                
                tableRow.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize);
            } else if ("4".equals(type)) {
                
                tableRow.setBackgroundColor(Color.parseColor("#FF7F00"));
                // tableRow.setBackgroundColor(Color.YELLOW);
                textView.setTextSize(mLeafTextSize);
            } else if ("5".equals(type)) {
                
                tableRow.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize * 1.3f);
            } else if ("6".equals(type)) {
                
                tableRow.setBackgroundColor(Color.RED);
                textView.setTextSize(mLeafTextSize * 0.8f);
            } else {
                textView.setTextSize(mLeafTextSize);
            }
            return tableLayout;
        }
    }
    
    public boolean isNumeric(String str) {
        Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$");
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
